import 'package:flutter/material.dart';
import 'package:gameplay_flutter/m_room/constants/room_assets.dart';
import 'package:gameplay_flutter/m_room/room_manager.dart';
import 'package:gameplay_flutter/m_room/room_page/room_bottom.dart';
import 'package:gameplay_flutter/m_room/room_page/room_menber.dart';
import 'package:gameplay_flutter/m_room/room_page/room_svga_queue_player.dart';
import 'package:get/get.dart';

import '../room_controller/room_page_tag_inherited.dart';
import '../room_main_controller.dart';
import '../room_controller/room_controller.dart';
import 'room_input.dart';
import 'room_mic_list.dart';
import 'room_msg.dart';
import 'room_top.dart';

class RoomPage extends StatefulWidget {
  final int index;

  RoomPage({Key? key, required this.index}) : super(key: key);

  @override
  _RoomPageState createState() => _RoomPageState();
}

class _RoomPageState extends State<RoomPage> {
  RoomMainController mainController = Get.put(RoomMainController());
  late final RoomController roomController;

  bool isCurrentIndex = false;

  // 在 _RoomPageState 类中添加变量
  Worker? _indexWorker; // 用于管理监听器

  @override
  void initState() {
    roomController = Get.put(RoomController(), tag: '${widget.index}');
    roomController.roomDataModel = mainController.roomDatas.value[widget.index];
    super.initState();
    roomController.extraInfo();
    roomController.micAll();
    roomController.messageSingleRoom();
    // 添加监听
    _indexWorker = ever(mainController.currentIndex, (value) {
      if (value == widget.index && !isCurrentIndex) {
        // 仅处理当前页面相关变化
        joinChannel();
        isCurrentIndex = true;
      }
      if (value != widget.index && isCurrentIndex) {
        // 仅处理当前页面相关变化
        leaveChannel();
        isCurrentIndex = false;
      }
    });
  }

  @override
  void dispose() {
    _indexWorker?.dispose();
    Get.delete<RoomController>(tag: '${widget.index}');
    if (isCurrentIndex) {
      leaveChannel();
    }
    super.dispose();
  }

  void joinChannel() {
    String roomNo = roomController.roomDataModel?.roomInfo?.roomNo ?? "";
    RoomManager.instance
        .joinChannel(roomNo,  roomController.handleMessage);
  }

  void leaveChannel() {
    String roomNo = roomController.roomDataModel?.roomInfo?.roomNo ?? "";
    String token = roomController.roomDataModel?.roomTokenBean ?? "";
    RoomManager.instance.leaveChannel(roomNo);
  }

  @override
  Widget build(BuildContext context) {
    return RoomPageTagInheritedWidget(
      tag: '${widget.index}',
      child: Stack(
        children: [
          Positioned.fill(
            child: Image.asset(
              RoomAssets.roomBg,
              fit: BoxFit.fill,
            ),
          ),
          SafeArea(
            child: Column(
              children: [
                RoomTop(),
                const RoomMenber(),
                const RoomMicList(),
                const RoomMsg(),
                const RoomBottom(),
              ],
            ),
          ),
          const RoomSvgaQueuePlayer(),
          const RoomInput(),
        ],
      ),
    );
  }
}
